package com.xcx.baseManger.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xcx.baseManger.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.HashMap;
import java.util.Map;

//websocket拦截器
@Component
public class MyHandshakeInterceptor implements HandshakeInterceptor {




    //握手之前，返回false，则不建立链接
    @Override
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> attributes) throws Exception {
        String path = serverHttpRequest.getURI().getPath();
        String[] split = StringUtils.split(path, '/');
        if (split.length != 2){
            return false;
        }
        attributes.put("ByUsername",split[1]);

//
//        //可以在session中放入一些数据
//        attributes.put("uid",1001);
//        System.out.println("开始握手。。。。。。。。");
        return true;
    }


    //握手成功后执行
    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }
}
